package com.party.core.service.article.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.UUIDUtils;
import com.party.core.dao.read.article.ArticleMemberReadDao;
import com.party.core.dao.write.article.ArticleMemberWriteDao;
import com.party.core.model.article.ArticleMember;
import com.party.core.service.article.IArticleMemberService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * Created by huangwuming
 * on 2018/6/13/013.
 */
@Service
public class ArticleMemberService implements IArticleMemberService{
    @Autowired
    private ArticleMemberWriteDao articleMemberWriteDao;
    @Autowired
    private ArticleMemberReadDao articleMemberReadDao;
    @Override
    public String insert(ArticleMember articleMember) {
        String uuid = UUIDUtils.generateRandomUUID();
        if (Strings.isNullOrEmpty(articleMember.getId())) {
            articleMember.setId(uuid);
        }
        boolean result = articleMemberWriteDao.insert(articleMember);
        if(result){
            return articleMember.getId();
        }
        return null;
    }

    @Override
    public boolean update(ArticleMember articleMember) {

        return articleMemberWriteDao.update(articleMember);
    }

    @Override
    public boolean deleteLogic(@NotNull String id) {
        return false;
    }

    @Override
    public boolean delete(@NotNull String id) {
        if(Strings.isNullOrEmpty(id)){
            return false;
        }
        return articleMemberWriteDao.deleteLogic(id);
    }

    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        return false;
    }

    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        return articleMemberWriteDao.batchDelete(ids);
    }

    @Override
    public ArticleMember get(String id) {
        return articleMemberReadDao.get(id);
    }

    @Override
    public List<ArticleMember> listPage(ArticleMember articleMember, Page page) {
        return articleMemberReadDao.listPage(articleMember ,page);
    }

    @Override
    public List<ArticleMember> list(ArticleMember articleMember) {
        return articleMemberReadDao.listPage(articleMember,null );
    }

    @Override
    public List<ArticleMember> batchList(@NotNull Set<String> ids, ArticleMember articleMember, Page page) {
        if (CollectionUtils.isEmpty(ids)){
            return Collections.EMPTY_LIST;
        }
        return articleMemberReadDao.batchList(ids ,new HashedMap() ,page);
    }

    @Override
    public void deleteByUnique(String articleId, String memberId) {
        articleMemberWriteDao.deleteByUnique(articleId,memberId);
    }

    @Override
    public void deleteByArticleId(String articleId) {
        articleMemberWriteDao.deleteByArticleId(articleId);
    }
}
